package com.cuit.springboot.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.cuit.springboot.gentry.Enterprise;
import com.cuit.springboot.service.EnterpriseService;
import com.cuit.springboot.util.MyException;
import com.cuit.springboot.util.PageInfo;
import com.cuit.springboot.util.ServerResponse;
import com.github.pagehelper.Page;

@Controller
@RequestMapping("enterprise")
public class EnterpriseController {

	private Logger logger = LoggerFactory.getLogger(EnterpriseController.class);

	
	@RequestMapping("/index")
	public String index() {
		return "index";
	}
	
	@RequestMapping("/select")
	public String select() {
		return "select";
	}
	
	@Autowired
	EnterpriseService enterpriseService;

	//分页查询所有
	@RequestMapping("selectAll")
	@ResponseBody
	public ServerResponse<?> selectAll(Integer pageNo) {
		Integer pageNos = 1;
		if (pageNo != null) {
			pageNos = pageNo;
		}
		Page<Enterprise> persons = enterpriseService.findByPage(pageNos, 10);
		// 需要把Page包装成PageInfo对象才能序列化。该插件也默认实现了一个PageInfo
		PageInfo<Enterprise> pageInfo = new PageInfo<>(persons);
		logger.debug(pageInfo.toString());
		return ServerResponse.createBySuccess(pageInfo);
	}
	
	//添加单个
	@RequestMapping("insert")
	@ResponseBody
	public ServerResponse<?> insert(Enterprise enterprise) {
		logger.debug(JSON.toJSONString(enterprise));
		if (enterprise == null) {
			new MyException("参数错误，请重试！");
		}
		enterpriseService.insert(enterprise);
		logger.debug(JSON.toJSONString(enterprise));
		return ServerResponse.createBySuccess();
	}

	//查询单个
	@RequestMapping("selectByOne")
	public ServerResponse<?> selectByOne(Integer id) {
		if (id == null) {
			new MyException("参数错误，请重试！");
		}
		Enterprise enterprise=enterpriseService.selectByOne(id);
		logger.debug(JSON.toJSONString(enterprise));
		return ServerResponse.createBySuccess(enterprise);
	}
	
}
